<?php
/**
 * @api {get} http://aicomm.bndvip.com/Api/Forum/labelList/ 获取话题
 * @apiName labelList
 * @apiGroup Forum
 *
 * @apiSuccess {Number} code  返回code(大于0为成功).
 */
/**
 * @api {get} http://aicomm.bndvip.com/Api/Forum/forumAdd/ 发帖
 * @apiName forumAdd
 * @apiGroup Forum
 *
 * @apiParam {Number} user_id 用户id(必填).
 * @apiParam {String} context 发布内容，汉字1000字以内(必填).
 * @apiParam {Number} hidden 是否为匿名发送 1为匿名 默认为非匿名(非必填).
 * @apiParam {Number} label_id 用户选择话题id(非必填).
 * @apiParam {String} image 图片(必填).
 * @apiSuccess {Number} code  返回code(大于0为成功).
 */
/**
 * @api {get} http://aicomm.bndvip.com/Api/Forum/forumList/ 帖子列表
 * @apiName forumList
 * @apiGroup Forum
 *
 * @apiParam {Number} user_id 用户id(必填).
 * @apiParam {Number} page 页码(非必填).
 * @apiParam {Number} label_id 存在为话题列表。不存在为最新列表(非必填).
 * @apiParam {Number} keyword 存在搜索(非必填).
 * @apiParam {String} address 地址(非必填).
 * @apiSuccess {Number} code  返回code(大于0为成功).
 */
/**
 * @api {get} http://aicomm.bndvip.com/Api/Forum/forumDetail/ 帖子/专题详情
 * @apiName forumDetail
 * @apiGroup Forum
 *
 * @apiParam {Number} id 帖子id(必填).
 * @apiParam {Number} user_id 用户id(必填).
 * @apiSuccess {Number} code  返回code(大于0为成功).
 */
/**
 * @api {get} http://aicomm.bndvip.com/Api/Forum/commentList/ 评论列表
 * @apiName commentList
 * @apiGroup Forum
 *
 * @apiParam {Number} id 帖子id(必填).
 * @apiParam {Number} page 页码(非必填).
 * @apiSuccess {Number} code  返回code(大于0为成功).
 */
/**
 * @api {get} http://aicomm.bndvip.com/Api/Forum/pinglunAdd/ 发表评论
 * @apiName pinglunAdd
 * @apiGroup Forum
 *
 * @apiParam {Number} pid 帖子id(必填).
 * @apiParam {Number} user_id 用户id(必填).
 * @apiParam {String} context 评论内容(必填).
 * @apiSuccess {Number} code  返回code(大于0为成功).
 */
/**
 * @api {get} http://aicomm.bndvip.com/Api/Forum/commentAdd/ 点赞
 * @apiName commentAdd
 * @apiGroup Forum
 *
 * @apiParam {Number} type 1为评论点赞，不为1为帖子点赞(非必填).
 * @apiParam {Number} user_id 用户id(必填).
 * @apiParam {Number} fid 帖子/评论id(必填).
 * @apiSuccess {Number} code  返回code(大于0为成功).
 */
/**
 * @api {get} http://aicomm.bndvip.com/Api/Forum/focusAdd/ 关注
 * @apiName focusAdd
 * @apiGroup Forum
 *
 * @apiParam {Number} buid 被关注人uid(必填).
 * @apiParam {Number} user_id 用户id(必填).
 * @apiSuccess {Number} code  返回code(大于0为成功).
 */
/**
 * @api {get} http://aicomm.bndvip.com/Api/Forum/focusLiat/ 关注列表
 * @apiName focusLiat
 * @apiGroup Forum
 *
 * @apiParam {Number} page 页码(非必填).
 * @apiParam {Number} user_id 用户id(必填).
 * @apiSuccess {Number} code  返回code(大于0为成功).
 */
/**
 * @api {get} http://aicomm.bndvip.com/Api/Forum/forumMy/ 个人中心
 * @apiName forumMy
 * @apiGroup Forum
 *
 * @apiParam {Number} page 页码(非必填).
 * @apiParam {Number} user_id 用户id(必填).
 * @apiSuccess {Number} code  返回code(大于0为成功).
 */
/**
 * @api {get} http://aicomm.bndvip.com/Api/Forum/fornmDel/ 删除帖子
 * @apiName fornmDel
 * @apiGroup Forum
 *
 * @apiParam {Number} id 帖子id(必填).
 * @apiParam {Number} user_id 用户id(必填).
 * @apiSuccess {Number} code  返回code(大于0为成功).
 */
/**
 * @api {get} http://aicomm.bndvip.com/Api/Forum/fornmFind/ 编辑 详情数据
 * @apiName fornmFind
 * @apiGroup Forum
 *
 * @apiParam {Number} id 帖子id(必填).
 * @apiParam {Number} user_id 用户id(必填).
 * @apiSuccess {Number} code  返回code(大于0为成功).
 */
/**
 * @api {get} http://aicomm.bndvip.com/Api/Forum/fornmFind/ 编辑 详情数据
 * @apiName fornmFind
 * @apiGroup Forum
 *
 * @apiParam {Number} id 帖子id(必填).
 * @apiParam {Number} user_id 用户id(必填).
 * @apiParam {Number} context 内容(必填).
 * @apiParam {Number} hidden 是否为匿名1为匿名(非必填).
 * @apiParam {Number} label_id 话题id(必填).
 * @apiParam {String} address 地址(非必填).
 * @apiSuccess {Number} code  返回code(大于0为成功).
 */
/**
 * @api {get} http://aicomm.bndvip.com/Api/Forum/zhuantiList/ 专题列表
 * @apiName zhuantiList
 * @apiGroup Forum
 *
 * @apiParam {Number} page 页码(非必填).
 * @apiSuccess {Number} code  返回code(大于0为成功).
 */
namespace app\api\controller;
use app\common\controller\Api;
use think\Cache;
use \think\Log;
use think\Db;
use think\Model;
use think\Session;

class Forum extends Api
{
    //获取话题
    public function labelList()
    {
        //获取当前话题
        $where['status'] = array('eq', 0);
        $where['is_del'] = array('eq', 0);
        $labelList = M('label')->field('id,name')->where($where)->order('rand()')->limit(10)->select();
        if ($labelList) {
            $return = codemsg(5003);
            $return['data'] = $labelList;
            Log::api('【Forum/labelList】-[return]--' . json_encode($return));
            echo json_encode($return);
            die;
        } else {
            $return = codemsg(-7028);
            Log::api('【Forum/labelList】-[return]--' . json_encode($return));
            echo json_encode($return);
            die;
        }
    }

    //发帖
    public function forumAdd()
    {
        $con = I('context');
        $is_hidden = I('hidden');
        $user_id = I('user_id');
        $label_id = I('label_id');
        $address = I('address');
        Log::api('【Forum/forumAdd】-[参数]--' . json_encode('con**' . $con . '-is_hidden**' . $is_hidden . '-user_id**' . $user_id . '-label_id**' . $label_id . '-address**' . $address));
        $file = request()->file('image');
        //用户
        if ($user_id <= 0 || empty($user_id)) {
            $return = codemsg(-9);
            Log::api('【Forum/forumadd】-[返回]--' . json_encode($return));
            echo json_encode($return);
            die;
        }
        if ($con) {
            if (strlen($con) > 3000) {
                $return = codemsg(-7035);
                Log::api('【Forum/forumadd】-[返回]--' . json_encode($return));
                echo json_encode($return);
                die;
            }
            $map['content'] = $con;
        } else {
            //未上传内容
            $return = codemsg(-7034);
            Log::api('【Forum/forumadd】-[返回]--' . json_encode($return));
            echo json_encode($return);
            die;
        }
        //话题id
        if ($label_id > 0 && !empty($label_id)) {
            $map['label_id'] = $label_id;
        }
        //是否匿名发送
        if ($is_hidden == 1) {
            //判断今天是否存在匿名发送
            $hiddenwhere['createtime'] = array('gt', strtotime(date('Y-m-d', time())));
            $hiddenwhere['uid'] = array('eq', $user_id);
            $hiddenwhere['is_del'] = array('eq', 0);
            $hiddenwhere['recycle'] = array('eq', 0);
            $hiddenwhere['is_hidden'] = array('eq', 1);
            $hiddenlist = M('dis_post')->where($hiddenwhere)->find();
            if ($hiddenlist) {
                // 上传失败获取错误信息
                $return = codemsg(-7036);
                Log::api('【Forum/forumadd】-[返回]--' . json_encode($return));
                echo json_encode($return);
                die;
            }
            $map['is_hidden'] = 1;
        }
        $map['uid'] = $user_id;
        $map['createtime'] = time();
        //上传图片
        if ($file) {
            $info = $file->move(ROOT_PATH . 'public' . DS . 'upload' . DS . 'sq');
            if ($info) {
                $url = '/public' . DS . 'upload' . DS . 'sq' . DS .$info->getSaveName();
                $map['logo'] = $url;
            } else {
                // 上传失败获取错误信息
                $return = codemsg(-7032);
                Log::api('【Forum/forumadd】-[返回]--' . json_encode($return));
                echo json_encode($return);
                die;
            }
        }
        if ($address) {
            $map['address'] = $address;
        }
        //添加发帖
        $add = M('dis_post')->insertGetId($map);
        if ($add) {
            $where['user_id'] = array('eq', $user_id);
            $map['dis_post_num'] = 'dis_post_num' + 1;
            $user = M('users')->where($where)->update();
            //将用户中数值加1
            if ($is_hidden == 1) {
                //匿名发送加1
                $userAdd = Db::execute('update tp_users set dis_post_num=dis_post_none+1 where user_id = ' . $user_id);
            } else {
                //正常发送
                $userAdd = Db::execute('update tp_users set dis_post_num=dis_post_num+1 where user_id = ' . $user_id);
            }
            $return = codemsg(3);
            $return['data'] = $add;
            Log::api('【Forum/forumadd】-[返回]--' . json_encode($return));
            echo json_encode($return);
            die;
        } else {
            $return = codemsg(-7007);
            Log::api('【Forum/forumadd】-[返回]--' . json_encode($return));
            echo json_encode($return);
            die;
        }

    }

    //帖子列表
    public function forumList()
    {
        $page = I('page');
        $user_id = I('user_id');
        //存在label_id 为话题列表
        $label_id = I('label_id');
        //存在keyword为搜索
        $keyword = I('keyword');
        //用户
        if ($user_id <= 0 || empty($user_id)) {
            $return = codemsg(-9);
            Log::api('【Forum/forumList】-[返回]--' . json_encode($return));
            echo json_encode($return);
            die;
        }
        if ($page <= 0 || empty($page)) {
            $page = 1;
        }
        //获取所有话题
        $labelwhere['is_del'] = array('eq', 0);
        $labelwhere['status'] = array('eq', 0);
        //搜索
        if ($keyword) {
            $labelwhere['name'] = array('like', '%' . $keyword . '%');
        }
        $labellist = M('label')->where($labelwhere)->column('id,name,status');
        if ($keyword) {
            if (empty($labellist)) {
                $return = codemsg(-7028);
                Log::api('【Forum/forumList】-[返回]--' . json_encode($return));
                echo json_encode($return);
                die;
            }
            $keywordlist = array();
            foreach ($labellist as $kkk => $vvv) {
                $keywordlist[] = $vvv['id'];
            }
            $where['p.label_id'] = array('in', $keywordlist);
        }
        $labellist[0] = array('id' => 0, 'name' => '');
        $star = ($page - 1) * 20;
        $end = $page * 20;
        $where['p.is_del'] = array('eq', 0);
        $where['p.recycle'] = array('eq', 0);
        $where['u.is_lock'] = array('eq', 0);
        $where['p.is_zhuanti'] = array('eq', 0);
        //话题
        if ($label_id) {
            $where['p.label_id'] = array('eq', $label_id);
        }
        //获取列表
        $data = M('dis_post')
            ->alias('p')
            ->field('p.*,u.nickname,u.head_pic')
            ->where($where)
            ->join('tp_users u', 'u.user_id=p.uid')
            ->order('p.createtime desc')
            ->limit($star, $end)
            ->select();
        //获取点赞
        $dataidlist = array();
        foreach ($data as $kk => $vv) {
            $dataidlist[] = $vv['id'];
        }

        $prawhere['uid'] = array('eq', $user_id);
        $prawhere['fid'] = array('in', $dataidlist);
        $praise = M('praise')->where($prawhere)->column('fid,id');
        $list = array();
        foreach ($data as $k => $v) {
            $list[$k] = $v;
            $list[$k]['lname'] = $labellist[$v['label_id']]['name'];
            if (!empty($praise[$v['id']])) {
                $list[$k]['dz'] = true;
            } else {
                $list[$k]['dz'] = false;
            }
            $time = 0;
            $time = round((time() - $v['createtime']) / 60);
            if ($time < 60) {
                $list[$k]['createtime'] = $time . "分钟前";
            } else {
                $list[$k]['createtime'] = date('Y-m-d', $v['createtime']);
            }
            if ($v['is_hidden'] == 1) {
                $list[$k]['nickname'] = "匿名";
                $list[$k]['head_pic'] = "public/upload/pic/default.jpg";
            }
        }
        if ($list) {
            $return = codemsg(5003);
            $return['data'] = $list;
            Log::api('【Forum/forumList】-[返回]--' . json_encode($return));
            echo json_encode($return);
            die;
        } else {
            $return = codemsg(-7028);
            Log::api('【Forum/forumList】-[返回]--' . json_encode($return));
            $return['data'] = array();
            echo json_encode($return);
            die;
        }
    }

    //帖子/专题详情
    public function forumDetail()
    {
        $id = I('id');
        $user_id = I('user_id');
        if ($id <= 0 || empty($id) || $user_id <= 0 || empty($user_id)) {
            $return = codemsg(-9);
            Log::api('【Forum/forumDetail】-[返回]--' . json_encode($return));
            echo json_encode($return);
            die;
        }
        $where['p.id'] = array('eq', $id);
        $where['p.is_del'] = array('eq', 0);
        $where['p.recycle'] = array('eq', 0);
        $where['u.is_lock'] = array('eq', 0);
        $data = M('dis_post')
            ->alias('p')
            ->field('p.*,u.nickname,u.head_pic')
            ->join('tp_users u', 'u.user_id=p.uid')
            ->where($where)
            ->find();
        if (empty($data)) {
            $return = codemsg(-7037);
            Log::api('【Forum/forumDetail】-[返回]--' . json_encode($return));
            echo json_encode($return);
            die;
        }
        //查询用户是否关注
        $focuswhere['uid'] = $user_id;
        $focuswhere['buid'] = $data['uid'];
        $focusdata = M('focus')->where($focuswhere)->find();
        if ($focusdata) {
            $is_focusdata = true;
        } else {
            $is_focusdata = false;
        }
        if ($data['is_hidden'] == 1) {
            $data['nickname'] = "匿名";
            $data['head_pic'] = "/public/upload/pic/default.jpg";
        }
        //将观看人数加1
        $pwhere['id'] = array('eq', $id);
        $map['count'] = $data['count'] + 1;
        $viewadd = M('dis_post')->where($pwhere)->update($map);
        //获取10条数据评论
        $plwhere['c.pid'] = array('eq', $data['id']);
            $plwhere['c.is_del'] = array('eq', 0);
        $plwhere['u.is_lock'] = array('eq', 0);
        $pcount = M('comment_tiezi')
            ->alias('c')
            ->join('tp_users u', 'u.user_id=c.uid')
            ->where($plwhere)
            ->count();
        $pdata = M('comment_tiezi')
            ->alias('c')
            ->field('c.*,u.nickname,u.head_pic')
            ->join('tp_users u', 'u.user_id=c.uid')
            ->where($plwhere)
            ->order('createtime desc')
            ->limit(10)
            ->select();
        if ($pdata) {
            foreach ($pdata as $k => $v) {
                $list[$k] = $v;
                $list[$k]['createtime'] = date('Y-m-d', $v['createtime']);
            }
        }
        if (empty($list)){
            $list = array();
        }
        //话题获取
        $label = array();
        if ($data['label_id'] > 0) {
            $lawhere['id'] = array('eq', $data['label_id']);
            $label = M('label')->where($lawhere)->find();
        }
        $return = codemsg(5003);
        $return['data']['detail'] = $data;
        $return['data']['pinglunlist'] = $list;
        $return['data']['is_focusdata'] = $is_focusdata;
        $return['data']['label'] = $label;
        $return['data']['pingluncount'] = $pcount;
        $return['data']['viewcount'] = $data['count'];
        Log::api('【Forum/forumDetail】-[返回]--' . json_encode($return));
        echo json_encode($return);
        die;
    }

    //评论列表
    public function commentList()
    {
        $id = I('id');
        if ($id <= 0 || empty($id)) {
            $return = codemsg(-9);
            Log::api('【Forum/commentList】-[返回]--' . json_encode($return));
            echo json_encode($return);
            die;
        }
        $page = I('page');
        if ($page <= 0 || empty($page)) {
            $page = 1;
        }
        $star = ($page - 1) * 10;
        $end = $page * 10;
        //获取10条数据评论
        $plwhere['c.pid'] = array('eq', $id);
        $plwhere['c.is_del'] = array('eq', 0);
        $plwhere['u.is_lock'] = array('eq', 0);
        $pdata = M('comment_tiezi')
            ->alias('c')
            ->field('c.*,u.nickname,u.head_pic')
            ->join('tp_users u', 'u.user_id=c.uid')
            ->where($plwhere)
            ->order('createtime desc')
            ->limit($star, $end)
            ->select();
        if ($pdata) {
            $return = codemsg(5003);
            $return['data'] = $pdata;
            Log::api('【Forum/commentList】-[返回]--' . json_encode($return));
            echo json_encode($return);
            die;
        } else {
            $return = codemsg(-7038);
            Log::api('【Forum/commentList】-[返回]--' . json_encode($return));
            echo json_encode($return);
            die;
        }

    }

    //发表评论
    public function pinglunAdd()
    {
        $pid = I('pid');
        $context = I('context');
        $uid = I('user_id');
        if ($pid <= 0 || empty($pid) || $uid <= 0 || empty($uid) || empty($context)) {
            $return = codemsg(-9);
            Log::api('【Forum/pinglunAdd】-[返回]--' . json_encode($return));
            echo json_encode($return);
            die;
        }
        //添加评论
        $map['pid'] = $pid;
        $map['context'] = $context;
        $map['uid'] = $uid;
        $map['createtime'] = time();
        $add = M('comment_tiezi')->insertGetId($map);
        //将评论的pid加+1
        $userAdd = Db::execute('update tp_dis_post set comment_num=comment_num+1 where id = ' . $pid);
        if (empty($add)) {
            $return = codemsg(-7021);
            Log::api('【Forum/pinglunAdd】-[返回]--' . json_encode($return));
            echo json_encode($return);
            die;
        } else {
            $return = codemsg(5007);
            Log::api('【Forum/pinglunAdd】-[返回]--' . json_encode($return));
            echo json_encode($return);
            die;
        }

    }

    //点赞  type = 1为评论点赞 不为1为帖子点赞  fid为帖子/评论 uid用户id
    public function commentAdd()
    {
        $fid = I('fid');
        $type = I('type');
        $uid = I('user_id');
        if ($fid <= 0 || empty($fid) || $uid <= 0 || empty($uid)) {
            $return = codemsg(-9);
            Log::api('【Forum/commentAdd】-[返回]--' . json_encode($return));
            echo json_encode($return);
            die;
        }
        //查询是否存在该赞
        $where['fid'] = array('eq', $fid);
        $where['uid'] = array('eq', $uid);
        if ($type == 1) {
            $where['is_commit'] = array('eq', 1);
            $dzmap['is_commit'] = 1;
        }else{
            $where['is_commit'] = array('eq', 0);
            $dzmap['is_commit'] = 0;
        }
        $find = M('praise')->where($where)->find();
        if ($find) {
            //存在则删除
            $save = M('praise')->where($where)->delete();
            $fwhere['id'] = array('eq', $fid);
            if ($type == 1) {
                $finddata = M('comment_tiezi')->where($fwhere)->find();
                if ($finddata) {
                    $map['dz'] = $finddata['dz'] - 1;
                    $savedata = M('comment_tiezi')->where($fwhere)->update($map);
                }
            } else {
                $finddata = M('dis_post')->where($fwhere)->find();
                if ($finddata) {
                    $map['dz'] = $finddata['dz'] - 1;
                    $savedata = M('dis_post')->where($fwhere)->update($map);
                }
            }
        } else {
            //不存在新增
            $dzmap['fid'] = $fid;
            $dzmap['uid'] = $uid;
            $save = M('praise')->insert($dzmap);
            $fwhere['id'] = array('eq', $fid);
            if ($type == 1) {
                $finddata = M('comment_tiezi')->where($fwhere)->find();
                if ($finddata) {
                    $map['dz'] = $finddata['dz'] + 1;
                    $savedata = M('comment_tiezi')->where($fwhere)->update($map);
                }
            } else {
                $finddata = M('dis_post')->where($fwhere)->find();
                if ($finddata) {
                    $map['dz'] = $finddata['dz'] + 1;
                    $savedata = M('dis_post')->where($fwhere)->update($map);
                }
            }
        }
        if ($save) {
            $return = codemsg(3);
            Log::api('【Forum/commentAdd】-[返回]--' . json_encode($return));
            echo json_encode($return);
            die;
        } else {
            $return = codemsg(-7021);
            Log::api('【Forum/commentAdd】-[返回]--' . json_encode($return));
            echo json_encode($return);
            die;
        }
    }

    //关注 uid用户id 被关注用户id
    public function focusAdd()
    {
        $uid = I('user_id');
        $buid = I('buid');
        if (empty($uid) || $uid <= 0 || $buid <= 0 || empty($buid)) {
            $return = codemsg(-9);
            Log::api('【Forum/focusAdd】-[返回]--' . json_encode($return));
            echo json_encode($return);
            die;
        }
        if ($buid == $uid) {
            $return = codemsg(-7040);
            Log::api('【Forum/focusAdd】-[返回]--' . json_encode($return));
            echo json_encode($return);
            die;
        }
        //查询是否存在该关注
        $where['uid'] = array('eq', $uid);
        $where['buid'] = array('eq', $buid);
        $find = M('focus')->where($where)->find();
        //查询被关注人是否存在
        $userwhere['user_id'] = array('eq', $buid);
        $userwhere['is_lock'] = array('eq', 0);
        $userfind = M('users')->where($userwhere)->find();
        if (empty($userfind)) {
            $return = codemsg(-7039);
            Log::api('【Forum/focusAdd】-[返回]--' . json_encode($return));
            echo json_encode($return);
            die;
        }
        if ($find) {
            //存在关注
            $save = M('focus')->where($where)->delete();
            $map['focus'] = $userfind['focus'] - 1;
            $usersave = M('users')->where($userwhere)->update($map);
            $userAdd = Db::execute('update tp_users set focus_num=focus_num-1 where user_id = ' . $buid);
        } else {
            //不存在关注
            $focusmap['uid'] = $uid;
            $focusmap['buid'] = $buid;
            $save = M('focus')->add($focusmap);
            $map['focus'] = $userfind['focus'] + 1;
            $usersave = M('users')->where($userwhere)->update($map);
            $userAdd = Db::execute('update tp_users set focus_num=focus_num+1 where user_id = ' . $buid);
        }
        if ($save) {
            $return = codemsg(3);
            Log::api('【Forum/focusAdd】-[返回]--' . json_encode($return));
            echo json_encode($return);
            die;
        } else {
            $return = codemsg(-7021);
            Log::api('【Forum/focusAdd】-[返回]--' . json_encode($return));
            echo json_encode($return);
            die;
        }
    }

    //关注列表
    public function focusLiat()
    {
        //查询该用户关注用户
        $uid = I('user_id');
        $page = I('page');
        if (empty($uid) || $uid <= 0) {
            $return = codemsg(-9);
            Log::api('【Forum/focusLiat】-[返回]--' . json_encode($return));
            echo json_encode($return);
            die;
        }
        if ($page <= 0 || empty($page)) {
            $page = 1;
        }
        $star = ($page - 1) * 15;
        $end = $page * 15;
        $where['f.uid'] = array('eq', $uid);
        $where['u.is_lock'] = array('eq', 0);
        $data = M('focus')
            ->alias('f')
            ->field('u.nickname,u.head_pic,u.signature,u.user_id,u.dis_post_num')
            ->join('tp_users u', 'u.user_id=f.buid')
            ->order('f.id desc')
            ->limit($star, $end)
            ->select();
        if ($data) {
            $return = codemsg(5003);
            $return['data'] = $data;
            Log::api('【Forum/focusLiat】-[返回]--' . json_encode($return));
            echo json_encode($return);
            die;
        } else {
            $return = codemsg(-7028);
            Log::api('【Forum/focusLiat】-[返回]--' . json_encode($return));
            echo json_encode($return);
            die;
        }
    }

    //个人中心
    public function forumMy()
    {
        $user_id = I('user_id');
        $page = I('page');
        if (empty($user_id) || $user_id <= 0) {
            $return = codemsg(-9);
            Log::api('【Forum/forumMy】-[返回]--' . json_encode($return));
            echo json_encode($return);
            die;
        }
        if ($page <= 0 || empty($page)) {
            $page = 1;
        }
        $star = ($page - 1) * 15;
        $end = $page * 15;
        //获取话题列表
        $wherelabel['status'] = array('eq', 0);
        $wherelabel['is_del'] = array('eq', 0);
        $labellist = M('label')->where($wherelabel)->column('id,name,status');
        //获取发表文章列表
        $where['is_del'] = array('eq', 0);
        $where['recycle'] = array('eq', 0);
        $where['uid'] = array('eq', $user_id);
        $data = M('dis_post')
            ->field('label_id,content,createtime,uid,id,dz,comment_num,count')
            ->where($where)
            ->order('createtime desc')
            ->limit($star, $end)
            ->select();
        $list = array();
        if ($data) {
            foreach ($data as $k => $v) {
                $list[$k] = $v;
                $list[$k]['label_name'] = $labellist[$v['label_id']]['name'];
                $list[$k]['createtime'] = date('Y-m-d H:i:s', $v['createtime']);
            }
        }
        if ($list) {
            $return = codemsg(5003);
            $return['data'] = $list;
            Log::api('【Forum/forumMy】-[返回]--' . json_encode($return));
            echo json_encode($return);
            die;
        } else {
            $return = codemsg(-7028);
            Log::api('【Forum/forumMy】-[返回]--' . json_encode($return));
            echo json_encode($return);
            die;
        }
    }

    //删除帖子
    public function fornmDel()
    {
        $user_id = I('user_id');
        $id = I('id');
        Log::api('【Forum/fornmDel】-[参数]--' . json_encode('user_id**' . $user_id . '-id' . $id));
        if ($user_id <= 0 || empty($user_id) || $id <= 0 || empty($id)) {
            $return = codemsg(-9);
            Log::api('【Forum/fornmDel】-[返回]--' . json_encode($return));
            echo json_encode($return);
            die;
        }
        $where['uid'] = array('eq', $user_id);
        $where['id'] = array('eq', $id);
        $map['recycle'] = 1;
        $map['is_del'] = 1;
        $del = M('dis_post')->where($where)->update($map);
        if ($del) {
            $return = codemsg(3);
            Log::api('【Forum/fornmDel】-[返回]--' . json_encode($return));
            echo json_encode($return);
            die;
        } else {
            $return = codemsg(-7021);
            Log::api('【Forum/fornmDel】-[返回]--' . json_encode($return));
            echo json_encode($return);
            die;
        }
    }

    //编辑 详情
    public function fornmFind()
    {
        $user_id = I('user_id');
        $id = I('id');
        Log::api('【Forum/fornmSave】-[参数]--' . json_encode('user_id**' . $user_id . '-id' . $id));
        if ($user_id <= 0 || empty($user_id) || $id <= 0 || empty($id)) {
            $return = codemsg(-9);
            Log::api('【Forum/fornmSave】-[返回]--' . json_encode($return));
            echo json_encode($return);
            die;
        }
        //加载数据
        $where['uid'] = array('eq', $user_id);
        $where['id'] = array('eq', $id);
        $where['id_del'] = array('eq', 0);
        $where['recycle'] = array('eq', 0);
        $data = M('dis_post')->where($where)->find();
        if ($data) {
            $return = codemsg(5003);
            $return['data'] = $data;
            Log::api('【Forum/fornmSave】-[返回]--' . json_encode($return));
            echo json_encode($return);
            die;
        } else {
            $return = codemsg(-7028);
            Log::api('【Forum/fornmSave】-[返回]--' . json_encode($return));
            echo json_encode($return);
            die;
        }
    }

    //编辑帖子
    public function fornmSave()
    {
        $user_id = I('user_id');
        $id = I('id');
        Log::api('【Forum/fornmSave】-[参数]--' . json_encode('user_id**' . $user_id . '-id' . $id));
        if ($user_id <= 0 || empty($user_id) || $id <= 0 || empty($id)) {
            $return = codemsg(-9);
            Log::api('【Forum/fornmSave】-[返回]--' . json_encode($return));
            echo json_encode($return);
            die;
        }
        $con = I('context');
        $is_hidden = I('hidden');
        $label_id = I('label_id');
        $address = I('address');
        $file = request()->file('image');
        //上传图片
        if ($file) {
            $info = $file->move(ROOT_PATH . 'public' . DS . 'upload' . DS . 'sq');
            if ($info) {
                $url = '\public' . DS . 'upload' . DS . 'sq' . DS .$info->getSaveName();
                $map['logo'] = $url;
            } else {
                // 上传失败获取错误信息
                $return = codemsg(-7032);
                Log::api('【Forum/fornmSave】-[返回]--' . json_encode($return));
                echo json_encode($return);
                die;
            }
        }
        if ($con) {
            $map['content'] = $con;
        }
        if ($label_id) {
            $map['label_id'] = $label_id;
        }
        if ($address) {
            $map['address'] = $address;
        }
        if ($is_hidden) {
            $map['is_hidden'] = $is_hidden;
        }
        $where['user_id'] = array('eq', $user_id);
        $where['id'] = array('eq', $id);
        $detail = M('dis_post')->where($where)->update($map);
        if ($detail) {
            $return = codemsg(3);
            Log::api('【Forum/fornmSave】-[返回]--' . json_encode($return));
            echo json_encode($return);
            die;
        } else {
            $return = codemsg(-7021);
            Log::api('【Forum/fornmSave】-[返回]--' . json_encode($return));
            echo json_encode($return);
            die;
        }
    }

    //专题列表
    public function zhuantiList()
    {
        $page = I('page');
        if ($page <= 0 || empty($page)) {
            $page = 1;
        }
        $star = ($page - 1) * 20;
        $end = $page * 20;
        $where['is_del'] = array('eq', 0);
        $where['recycle'] = array('eq', 0);
        $where['is_zhuanti'] = array('eq', 1);
        $data = M('dis_post')
            ->where($where)
            ->limit($star, $end)
            ->order('createtime desc')
            ->select();
        if ($data) {
            $return = codemsg(5003);
            $return['data'] = $data;
            Log::api('【Forum/zhuantiList】-[返回]--' . json_encode($return));
            echo json_encode($return);
            die;
        } else {
            $return = codemsg(-7038);
            Log::api('【Forum/zhuantiList】-[返回]--' . json_encode($return));
            echo json_encode($return);
            die;
        }
    }
}